package com.sp2.dev.test.mongodb;

import com.mongodb.*;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * Created by IntelliJ IDEA.
 * User: zhimingli
 * Date: 14-2-11
 * Time: 下午6:22
 * To change this template use File | Settings | File Templates.
 */
public class MongdbCURD extends MongodbConnection {
    //    protected static final String HOST = "127.0.0.1:27017";
//
//    protected static final String dbName = "test";
//    protected static final String tableName = "tableName";
//    protected static Mongo mongo;
//    protected static DB db;
//    protected static DBCollection users;
    public static void main(String[] args) {

    }

    //插入普通字符
    @Test
    public void addOther() {
        for (int i = 0; i < 100; i++) {
            DBObject dbObject = new BasicDBObject();
            dbObject.put("other", "other" + i);
            users.save(dbObject);
        }
    }

    //插入普通字符
    @Test
    public void add() {
        for (int i = 0; i < 100; i++) {
            DBObject dbObject = new BasicDBObject();
            dbObject.put("20120212", "20120212" + i);
            users.save(dbObject);
        }
    }

    /**
     * 插入
     * {
     * "name":"Mongodb",
     * "type":"type",
     * "count":1,
     * "info":{
     * x:200,
     * y:200
     * }
     * }
     */
    @Test
    public void add2() {
        DBObject doc = new BasicDBObject();
        doc.put("name", "Mongodb");
        doc.put("type", "type");
        doc.put("count", 1);
        DBObject info = new BasicDBObject();
        info.put("x", 200);
        info.put("y", 300);
        doc.put("info", info);
        users.save(doc);
    }

    //add3
    @Test
    public void add3() {
        DBObject doc = BasicDBObjectBuilder.start()
                .add("build", "build1").add("seconde", "seconde").get();
        users.save(doc);
    }

    // addList { "_id" : { "$oid" : "52fadb34925dcddd5fbf83c4"} , "list" : [ 1 , 2 , { "foo" : "bar"} , 4]}
    @Test
    public void addList() {
        List<Object> list = new ArrayList<Object>();
        list.add(1);
        list.add(2);
        list.add(new BasicDBObject("foo", "bar"));
        list.add(4);
        DBObject dbObject = new BasicDBObject("list", list);
        users.save(dbObject);
    }

    @Test
    public void find() {
        Pattern other7 = Pattern.compile("other7*", Pattern.CASE_INSENSITIVE);
        DBCursor cursor = users.find(new BasicDBObject("other", other7));
        while (cursor.hasNext()) {
            System.out.println(cursor.next());
        }
    }

    @Test
    public void findOne() {
        DBObject dbObject = users.findOne(new BasicDBObject("list", 1));
        System.out.println(dbObject);
    }

    @Test
    public void remover() {
        WriteResult result = users.remove(new BasicDBObject("other", "other0"));
        int i = result.getN();//1成功 0失败
        System.out.println(i);
    }

    //正则删除 hotel
    @Test
    public void remover2() {
        Pattern pattern = Pattern.compile(".*", Pattern.CASE_INSENSITIVE);
        WriteResult result = users.remove(new BasicDBObject("CName", pattern));
        int i = result.getN();//1成功 0失败
        System.out.println(i);
    }
}
